/*******************************************************************************

This code file takes the cleaned PLUTO data and merges in several more datasets
whose cleaning files themselves use the cleaning PLUTO data, producing a final
dataset for most analyses in the paper.

*******************************************************************************/

*** Manage settings

	run "~/Dropbox (MIT)/Research/NYC421a/code/modules/settings.do"

*** Load cleaned PLUTO data
	
	use "$data/clean/pluto_merged.dta", clear
	
*** Merge in additional datasets

	* Merge in contemporaneous share taxable

		merge 1:1 bbl using "$data/raw/assessments/av_fraction_mostrecent.dta", nogen keep(1 3)
			
	* Add special-zone indicators

		* Inclusionary Housing Program (IHP)
		merge 1:1 bbl using "$data/raw/special_areas/ihp_by_bbl.dta", nogen keep(1 3)
		replace ihp = 0 if (ihp == 1 & issuance_date < date_ihp) | missing(ihp)
		replace ihp = 1 if zonedist == 3 | zonedist == 4
		drop date_ihp

		* Neighborhood Preservation Program (NPP)
		merge 1:1 bbl using "$data/raw/special_areas/NPP_by_BBL.dta", nogen keep(1 3)	
		
		* Liberty Zone (runs out after July 2006)
		merge 1:1 bbl using "$data/raw/special_areas/LibertyZone.dta", nogen keep(1 3)
		replace libertyzone = 0 if issuance_date >= 17379
		
		* Greenpoint-Williamsburg GEA (exists from May 11, 2005 to July 1, 2008)
		merge 1:1 bbl using "$data/raw/special_areas/Greenpoint_Williamsburg_GEA.dta", keep(1 2 3)
		bys borough block: egen gw_gea_ = max(gw_gea)
		drop gw_gea
		rename gw_gea_ gw_gea
		replace gw_gea = 0 if issuance_date > 17713 | issuance_date < 16567 | missing(gw_gea)
		drop if _m == 2
		drop _m
				
	* NPP areas receive 421-a tax exemption as-of-right, thus no inclusionary units
	* Buildings w/ 1-4 units also do not qualify for 421-a 
	
		replace inclusionary_onsite = 0 if (npp == 1 & yearpermit < 2008) | unitsres < 5
		
	* Taxes
		
		run "$code/modules/dtaxrate_calc.do"
		
	* Additional variable labels

		label variable ihp "Eligible for Inclusionary Housing Program"
		label variable dtaxrate_onsite "PDV Difference in Tax Rate from On-Site Inclusionary"
		label variable dtaxrate_offsite "PDV Difference in Tax Rate from Off-Site Inclusionary"
			
*** Save data

	save "$data/clean/cleaned_data.dta", replace
